/*
 * TOP SECRET Copyright 2006-2015 Transsion.com All right reserved. This software is the confidential and proprietary
 * information of Transsion.com ("Confidential Information"). You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license agreement you entered into with Transsion.com.
 */
package com.yunji.framework_template.biz.service;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.yunji.framework_template.common.http.HttpConnection;
import com.yunji.framework_template.common.util.DateUtil;
import com.yunji.framework_template.common.util.StringUtil;
import com.yunji.framework_template.common.web.HttpResult;
import lombok.extern.slf4j.Slf4j;

/**
 * ClassName:AppannieService <br/>
 * Date: 2018年12月25日 下午4:08:06 <br/>
 * 
 * @author fenglibin1982@163.com
 * @Blog http://blog.csdn.net/fenglibing
 * @version
 * @see
 */
@Slf4j
@Service
public class AppannieService {

    @Value("#{settings['countries']}")
    private String countries;

    /**
     * 第一步打开登陆页面，目的是获取csrftoken，后续的请求会用上
     */
    private HttpResult doLoginStep1() throws ClientProtocolException, IOException {
        String url = "https://www.appannie.com/account/login/";
        Map<String, String> header = new HashMap<String, String>();
        header.put("content-language", "en");
        header.put("User-Agent",
                   "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36");
        HttpResult response = HttpConnection.doGet(url, null, header);
        return response;
    }

    /**
     * 执行登陆操作
     */
    private HttpResult doLoginStep2(HttpResult result) throws ClientProtocolException, IOException {
        String url = "https://www.appannie.com/account/login/";
        Map<String, String> header = new HashMap<String, String>();
        header.put("authority", "www.appannie.com");
        header.put("method", "POST");
        header.put("scheme", "https");
        header.put("path", "/account/login/");
        header.put("pragma", "no-cache");
        header.put("cache-control", "no-cache");
        header.put("origin", "https://www.appannie.com");
        header.put("upgrade-insecure-requests", "1");
        header.put("content-type", "application/x-www-form-urlencoded");
        header.put("user-agent",
                   "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36");
        header.put("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
        header.put("referer", "https://www.appannie.com/account/login/");
        header.put("accept-encoding", "gzip, deflate, br");
        header.put("accept-language", "q=0.9,en-US;q=0.8,en;q=0.7");

        Map<String, String> datas = new HashMap<String, String>();
        datas.put("username", "56553655@163.com");
        datas.put("password", "flb1212121212");
        datas.put("next", "/dashboard/home/");
        datas.put("csrfmiddlewaretoken", result.getOneCookie("csrftoken"));
        datas.put("content-language", "en");

        return HttpConnection.doPost(url, datas, header, result.getCookieStore());
    }

    /**
     * 执行数据获取
     */
    private HttpResult doGetData(HttpResult result, String countryCode) throws ClientProtocolException, IOException {
        String url = "https://www.appannie.com/ajax/top-chart/table/?market=google-play&country_code=%s&category=1&date=%s&rank_sorting_type=rank&page_size=500&order_type=desc";
        String date = DateUtil.formatDate(DateUtil.addDays(new Date(), -1), DateUtil.ISO_DATE_FORMAT);
        url = String.format(url, countryCode, date);
        Map<String, String> header = new HashMap<String, String>();
        header.put("authority", "www.appannie.com");
        header.put("pragma", "no-cache");
        header.put("cache-control", "no-cache");
        header.put("upgrade-insecure-requests", "1");
        header.put("user-agent",
                   "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36");
        header.put("accept", "application/json, text/plain, */*");
        header.put("accept-encoding", "gzip, deflate, br");
        header.put("referer", url);
        header.put("accept-language", "q=0.9,en-US;q=0.8,en;q=0.7");
        header.put("x-csrftoken", result.getOneCookie("csrftoken"));
        header.put("x-requested-with", "XMLHttpRequest");
        header.put("content-language", "en");

        CookieStore cs = new BasicCookieStore();
        cs.addCookie(new BasicClientCookie("sessionId", result.getOneCookie("sessionId")));
        cs.addCookie(new BasicClientCookie("aa_language", "en"));
        cs.addCookie(new BasicClientCookie("django_language", "en"));

        cs.addCookie(new BasicClientCookie("optimizelyEndUserId", result.getOneCookie("optimizelyEndUserId")));
        cs.addCookie(new BasicClientCookie("referrer", "https://www.appannie.com/apps/google-play/top-chart"));
        cs.addCookie(new BasicClientCookie("_gcl_au", result.getOneCookie("_gcl_au")));
        cs.addCookie(new BasicClientCookie("_ga", result.getOneCookie("_ga")));
        cs.addCookie(new BasicClientCookie("_mkto_trk", result.getOneCookie("_mkto_trk")));
        cs.addCookie(new BasicClientCookie("__adroll_fpc", result.getOneCookie("__adroll_fpc")));
        // cs.addCookie(new BasicClientCookie("__idcontext", result.getOneCookie("__idcontext")));
        cs.addCookie(new BasicClientCookie("__ar_v4", result.getOneCookie("__ar_v4")));
        cs.addCookie(new BasicClientCookie("rid", result.getOneCookie("rid")));
        cs.addCookie(new BasicClientCookie("_gid", result.getOneCookie("_gid")));

        cs.addCookie(new BasicClientCookie("_fbp", result.getOneCookie("_fbp")));
        cs.addCookie(new BasicClientCookie("_gat_UA", result.getOneCookie("_gat_UA")));
        cs.addCookie(new BasicClientCookie("aa_user_token", result.getOneCookie("aa_user_token")));

        return HttpConnection.doGet(url, null, header, result.getCookieStore());
    }

    /**
     * 从AppAnnie抓取应用排行数据，返回获取到的Json串
     * 
     * @param countryCode
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public String getData(String countryCode) throws ClientProtocolException, IOException {
        log.info("开始从AppAnnie抓取国家：" + countryCode + "的数据.");
        HttpResult result = doLoginStep1();
        result = doLoginStep2(result);
        result = doGetData(result, countryCode);
        String content = result.getContent();
        if (StringUtil.isEmpty(content)) {
            return content;
        }
        log.info("抓取的内容长度为：" + content.length());
        if (content.length() < 1024) {
            log.info("抓取到的内容为：" + content);
        }
        return content;
    }

}
